package com.iplant.ams.controller.dms.scada;

import com.iplant.ams.service.DMSService;
import com.iplant.ams.service.po.dms.scada.alarm.DMSDeviceAlarm;
import com.iplant.ams.service.po.dms.scada.alarm.DMSDeviceAlarmStatistics;
import com.iplant.base.controller.BaseController;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.basic.service.po.bms.BMSEmployee;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/DMSDeviceAlarm")
public class DMSDeviceAlarmController extends BaseController {


    @Autowired
    DMSService mDMSService;

    public DMSDeviceAlarmController() {
        super();
    }


    @GetMapping("/All")
    public Object All(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            String wName = StringUtils.parseString(request.getParameter("Name"));
            String wAssetNo = StringUtils.parseString(request.getParameter("AssetNo"));
            int wDeviceType = StringUtils.parseInt(request.getParameter("DeviceType"), -1);
            int wModelID = StringUtils.parseInt(request.getParameter("ModelID"), -1);
            int wFactoryID = StringUtils.parseInt(request.getParameter("FactoryID"), -1);
            int wWorkShopID = StringUtils.parseInt(request.getParameter("WorkShopID"), -1);
            int wAreaID = StringUtils.parseInt(request.getParameter("AreaID"), -1);
            String wAlarmCode = StringUtils.parseString(request.getParameter("AlarmCode"));
            int wPositionID = StringUtils.parseInt(request.getParameter("PositionID"), -1);
            int wEventType = StringUtils.parseInt(request.getParameter("EventType"), -1);
            int wDataGrad = StringUtils.parseInt(request.getParameter("DataGrad"), -1);
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));

            Pagination wPagination = GetPagination(request);

            ServiceResult<List<DMSDeviceAlarm>> wServiceResult = mDMSService.DMS_SelectDeviceAlarmList(wBMSEmployee, wName,
                    wAssetNo, wDeviceType, wModelID, wFactoryID, wWorkShopID, wAreaID, wAlarmCode, wEventType, wDataGrad,
                    wPositionID, wStartTime, wEndTime, wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.getResult(), wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), wServiceResult.getResult(), null);
            }
        } catch (Exception ex) {
            log.error("All", ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.getMessage());
        }
        return wResult;
    }

    @GetMapping("/Current")
    public Object Current(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            int wDeviceID = StringUtils.parseInt(request.getParameter("DeviceID"), -1);
            String wDeviceNo = StringUtils.parseString(request.getParameter("DeviceNo"));
            String AssetNo = StringUtils.parseString(request.getParameter("AssetNo"));

            int wDeviceType = StringUtils.parseInt(request.getParameter("DeviceType"), -1);
            int wModelID = StringUtils.parseInt(request.getParameter("ModelID"), -1);
            int wFactoryID = StringUtils.parseInt(request.getParameter("FactoryID"), -1);
            int wWorkShopID = StringUtils.parseInt(request.getParameter("WorkShopID"), -1);
            int wAreaID = StringUtils.parseInt(request.getParameter("AreaID"), -1);
            String wAlarmCode = StringUtils.parseString(request.getParameter("AlarmCode"));
            int wDataGrad = StringUtils.parseInt(request.getParameter("DataGrad"), -1);
            int wPositionID = StringUtils.parseInt(request.getParameter("PositionID"), -1);

            ServiceResult<List<DMSDeviceAlarm>> wServiceResult = mDMSService.DMS_CurrentDeviceAlarmList(wBMSEmployee, wDeviceID, wDeviceNo, AssetNo,
                    wDeviceType, wModelID, wFactoryID, wWorkShopID,  wAreaID, wAlarmCode,wDataGrad, wPositionID);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.getResult(), null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), wServiceResult.getResult(), null);
            }
        } catch (Exception ex) {
            log.error("Current", ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.getMessage());
        }
        return wResult;
    }

    @GetMapping("/DeviceInfo")
    public Object DeviceInfo(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            int wID = StringUtils.parseInt(request.getParameter("DeviceID"), -1);
            String wDeviceNo = StringUtils.parseString(request.getParameter("DeviceNo"));
            String AssetNo = StringUtils.parseString(request.getParameter("AssetNo"));

            String wAlarmCode = StringUtils.parseString(request.getParameter("AlarmCode"));
            int wDataGrad = StringUtils.parseInt(request.getParameter("DataGrad"), -1);
            int wEventType = StringUtils.parseInt(request.getParameter("EventType"), -1);
            int wPositionID = StringUtils.parseInt(request.getParameter("PositionID"), -1);
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));
            wEndTime.add(Calendar.DAY_OF_MONTH,1);
            Pagination wPagination = GetPagination(request);

            ServiceResult<List<DMSDeviceAlarm>> wServiceResult = mDMSService.DMS_SelectDeviceAlarm(wBMSEmployee, wID,
                    wDeviceNo, AssetNo,wAlarmCode ,wEventType,wDataGrad,wPositionID, wStartTime, wEndTime, wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), wServiceResult.Result, null);
            }
        } catch (Exception ex) {
            log.error("DeviceInfo", ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.getMessage());
        }
        return wResult;
    }

    @GetMapping("/DeviceAll")
    public Object DeviceAll(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            List<Integer> wIDList = StringUtils.parseIntList(request.getParameter("DeviceIDList"), ",");

            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));

            String wAlarmCode = StringUtils.parseString(request.getParameter("AlarmCode"));
            int wDataGrad = StringUtils.parseInt(request.getParameter("DataGrad"), -1);
            int wEventType = StringUtils.parseInt(request.getParameter("EventType"), -1);
            int wPositionID = StringUtils.parseInt(request.getParameter("PositionID"), -1);

            Pagination wPagination = GetPagination(request);


            ServiceResult<List<DMSDeviceAlarm>> wServiceResult = mDMSService.DMS_SelectDeviceAlarmList(wBMSEmployee,
                    wIDList,wAlarmCode ,wEventType,wDataGrad,wPositionID, wStartTime, wEndTime, wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), wServiceResult.Result, null);
            }
        } catch (Exception ex) {
            log.error("DeviceAll", ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.getMessage());
        }
        return wResult;
    }

    @GetMapping("/Statistics")
    public Object Statistics(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);


            int wDeviceID = StringUtils.parseInt(request.getParameter("DeviceID"), -1);
            String wDeviceNo = StringUtils.parseString(request.getParameter("DeviceNo"));
            String wDeviceName = StringUtils.parseString(request.getParameter("DeviceName"));
            String wAssetNo = StringUtils.parseString(request.getParameter("AssetNo"));
            int wDeviceType = StringUtils.parseInt(request.getParameter("DeviceType"), -1);
            int wModelID = StringUtils.parseInt(request.getParameter("ModelID"), -1);
            int wFactoryID = StringUtils.parseInt(request.getParameter("FactoryID"), -1);
            int wWorkShopID = StringUtils.parseInt(request.getParameter("WorkShopID"), -1);
            int wAreaID = StringUtils.parseInt(request.getParameter("AreaID"), -1);
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));
            wEndTime.add(Calendar.DAY_OF_MONTH,1);
            int wStatus = StringUtils.parseInt(request.getParameter("Status"), -1);

            Pagination wPagination = GetPagination(request);

            ServiceResult<List<DMSDeviceAlarmStatistics>> wServiceResult = mDMSService.DMS_SelectDeviceAlarmStatisticsList(wBMSEmployee, wDeviceID, wDeviceNo, wDeviceName,
                    wAssetNo, wDeviceType, wModelID, wFactoryID, wWorkShopID,  wAreaID, wStatus, wStartTime, wEndTime, wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.getResult(), wPagination.getTotalPage());

                SetResult(wResult, "Frequency", wServiceResult.Get("Frequency",null));
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), wServiceResult.getResult(), null);
            }
        } catch (Exception ex) {
            log.error("Statistics", ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.getMessage());
        }
        return wResult;
    }


    @PostMapping("/SyncAlarmCurrent")

    public Object SyncAlarmCurrent(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
             

            BMSEmployee wBMSEmployee = GetSession(request);
            if (!wParam.containsKey("data") || !wParam.containsKey("AssetNoList")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            List<DMSDeviceAlarm> wDMSDeviceAlarmList = CloneTool.CloneArray(wParam.get("data"),DMSDeviceAlarm.class);

            List<String> wAssetNoList = CloneTool.CloneArray(wParam.get("AssetNoList"),String.class);

            ServiceResult<List<String>> wServerRst = mDMSService.DMS_SyncDeviceAlarmList(wBMSEmployee, wAssetNoList, wDMSDeviceAlarmList);
            //数据库当前报警与数据当前报警对比 有变更则查询最后一条历史记录 与数据库当前报警对比 更新结束时间并添加变更历史记录
            //数据库当前报警变更为当前报警

            if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServerRst.getResult(), null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), wServerRst.getResult(), null);
            }
        } catch (Exception ex) {
            log.error("SyncAlarmCurrent", ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.getMessage());
        }
        return wResult;

    }

    @PostMapping("/SyncAlarmHistory")
    public Object SyncAlarmHistory(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wBMSEmployee = GetSession(request);
            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            List<DMSDeviceAlarm> wDMSDeviceAlarmList = CloneTool.CloneArray (wParam.get("data"),DMSDeviceAlarm.class);
            ServiceResult<List<String>> wServerRst = mDMSService.DMS_SyncDeviceAlarmHistoryList(wBMSEmployee, wDMSDeviceAlarmList);


            if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServerRst.getResult(), null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode(), wServerRst.getResult(), null);
            }
        } catch (Exception ex) {
            log.error("SyncAlarmHistory", ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.getMessage());
        }
        return wResult;
    }


}
